<template>
  <div style="margin-top:2rem;margin-bottom:2rem;">
    <el-row>
      <el-col :span="21">
        <el-button type="primary" @click="manageAttachment()">附件管理</el-button>
        <el-button type="primary" style="margin-left:1rem;" @click="showWrittenExaminationInvitation">笔试邀请</el-button>
      </el-col>
      <el-col :span="3">
        <el-button type="primary" @click="newEmailDict">新增</el-button>
      </el-col>
    </el-row>
    <el-row style="margin-top:2rem;">
      <el-col>
        <el-table :data="tableData" style="width: 100%">
          <el-table-column label="模板名" width="120" prop="name">
          </el-table-column>
          <el-table-column label="收件人" prop="sendTo">
          </el-table-column>
          <el-table-column label="抄送" prop="cc">
          </el-table-column>
          <el-table-column label="前缀" prop="subjectPrefix">
          </el-table-column>
          <el-table-column label="内容" prop="content">
          </el-table-column>
          <el-table-column label="附件" prop="filename">
          </el-table-column>
          <el-table-column label="创建时间" width="180" prop="createTime">
          </el-table-column>
          <el-table-column label="更新时间" width="180" prop="updateTime">
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button size="mini" @click="editEmailDict(scope.row)">编辑</el-button>
              <el-button size="mini" type="danger" @click=" deleteOne(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <New ref="new" @fnc="getData"/>
    <Edit ref="edit" @fnc="getData"/>
    <Attachment ref="attachment" @fnc="getData"/>
    <writtenExaminationInvitation ref="writtenExaminationInvitation" @fnc="showWrittenExaminationInvitation"/>
  </div>
</template>
<script>
  import New from './email-new'
  import Edit from './email-edit'
  import Attachment from './attachment-manager.vue'
  import writtenExaminationInvitation from './writtenExaminationInvitation.vue'
  import _ from 'lodash'
  export default {
    components: {
      New,
      Edit,
      Attachment,
      writtenExaminationInvitation
    },
    data: function () {
      return {
        tableData: [],
      }
    },
    methods: {
      getData() {
        this.$http({
          url: `${this.$http.adornUrl('/emailDict/list?token='+this.$cookie.get('token') )}`,
          method: 'get',
        }).then(data => {
          this.tableData = data.data.data
        })
      },
      newEmailDict(){
          this.$refs.new.init()
      },
      editEmailDict(row){
          let data = _.cloneDeep(row)
          this.$refs.edit.init(data)
      },
      manageAttachment(){
        this.$refs.attachment.init()
      },
      showWrittenExaminationInvitation(){
        this.$refs.writtenExaminationInvitation.init()
      },
      //删除
      deleteOne(row) {
        this.rowData = row
        this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: `${this.$http.adornUrl('/emailDict/'+row.id+'?token='+this.$cookie.get('token') )}`,
            method: 'delete',
          }).then(({
            data
          }) => {
            if (data['code'] == 0) {
              this.$message({
                message: '删除成功',
                type: 'success'
              });
              this.getData()
            } else {
              this.$message({
                message: '数据删除错误',
                type: 'error'
              });
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
    },
    created() {
      this.getData()
    }
  }

</script>
